package org.jet.emall.security.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jet.emall.prod.po.Customer;
import org.jet.emall.prod.service.mp.ICustomerService;
import org.jet.emall.security.model.UserDetailsModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-07 19:45
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private ICustomerService customerService;

    /**
     * 根据用户名将用户信息加载出来，包装成UserDetails对象交给spring-security框架
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Customer customer = customerService.getOne(new QueryWrapper<Customer>().eq("username", username));
        if(customer==null){
            return null;
        }

        return new UserDetailsModel().setId(customer.getId())
                .setUsername(customer.getUsername())
                .setPassword(customer.getPassword())
                .setLocked(customer.getLocked());
    }
}
